using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for AccountDAO
/// </summary>
public class AccountDAO:DBAccess
{
    #region sql command
    string SQL_GET_ID = "select @@IDENTITY from dbo.members";
    string SQL_GET_ALL = "select * from dbo.memebers";
    string SQL_GET_ACCOUNT = "select * from dbo.members where member_login=@username";
    string SQL_INSERT = "insert into dbo.members(member_login, member_password,member_level, first_name, last_name, email, phone, address, card_type_id, card_number) values(@username, @password,@level, @firstname, @lastname, @email, @phone, @address, @cardtypeid, @cardnumber)";
    string SQL_UPDATE = "update dbo.members set first_name=@firstname, last_name= @lastname, email=@email, phone=@phone, addesss=@address, card_type_id=@cardtypeid,card_number=@cardnumber";
    string SQL_DELETE = "delete from dbo.members where member_login=@username, member_password=@password";
    #endregion
    #region fields
    string message = "";
    #endregion
    #region properties
    public string Message { get { return message; } }
    #endregion
    public AccountDAO()
	{
		//
		// TODO: Add constructor logic here
		//
    }
    #region methods
    public MemberInfo GetAccount(string username)
    {
        MemberInfo member = new MemberInfo();
        SqlCommand command = new SqlCommand(SQL_GET_ACCOUNT, conn);
        if (Connect())
        {
            command.Parameters.AddWithValue("@username",username);
            SqlDataReader dr=command.ExecuteReader();
            
            if(dr.Read()){
                member.Username = dr.GetString(0);
                member.Password = dr.GetString(1);
                member.Level = dr.GetInt32(2);
                member.FirstName = dr.GetString(3);
                member.LastName = dr.GetString(4);
                member.Email = dr.GetString(5);
               if(!dr.IsDBNull(6)) member.Phone = dr.GetString(6);
               if (!dr.IsDBNull(7)) member.Address = dr.GetString(7);
               if (!dr.IsDBNull(8)) member.Note = dr.GetString(8);
               if (!dr.IsDBNull(9)) member.Card.Type.CardTypeID = dr.GetInt32(9);
               if (!dr.IsDBNull(10)) member.Card.CardNumber = dr.GetString(10);
            }
            Disconnect();
        }
        return member;
    }
    public int Insert(MemberInfo member)
    {
        int effectedRows = 0;
        SqlCommand command = new SqlCommand(SQL_INSERT,conn);
        command.Parameters.AddWithValue("@username",member.Username);
        command.Parameters.AddWithValue("@password", member.Password);
        command.Parameters.AddWithValue("@level", member.Level);
        command.Parameters.AddWithValue("@firstname",member.FirstName);
        command.Parameters.AddWithValue("@lastname", member.LastName);
        command.Parameters.AddWithValue("@email", member.Email);
        command.Parameters.AddWithValue("@phone", member.Phone);
        command.Parameters.AddWithValue("@address", member.Address);
        command.Parameters.AddWithValue("@cardtypeid", member.Card.Type.CardTypeID);
        command.Parameters.AddWithValue("@cardnumber",member.Card.CardNumber);
        if (Connect())
        {
            effectedRows = command.ExecuteNonQuery();
            Disconnect();
        }
        return effectedRows;
    }
    #endregion
}
